<template>
  <div>
    <el-card class="box-card">
      <div class="addGoods">
        <el-button type="primary" @click="addToast">添加商品分类</el-button>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%; margin-bottom: 20px"
        row-key="cat_id"
        border
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column type="index" label="#" width="50"></el-table-column>
        <el-table-column prop="cat_name" label="分类名称"> </el-table-column>
        <el-table-column label="是否有效">
          <i class="el-icon-success"></i>
        </el-table-column>
        <el-table-column label="排序">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.cat_level == 0">一级</el-tag>
            <el-tag type="warning" v-else-if="scope.row.cat_level == 1"
              >二级</el-tag
            >
            <el-tag type="info" v-else-if="scope.row.cat_level == 2"
              >三级</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="edit(scope.row.cat_id, scope.row.cat_name)"
              >编辑</el-button
            >
            <el-button type="danger" @click="deleteTip(scope.row.cat_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <PageView :tableData="tableData" :total="total"></PageView>
    </el-card>
    <!-- 点击编辑的弹出框 -->
    <el-dialog title="更新分类名称" :visible.sync="editFlag">
      <el-form :model="form" :rules="rules">
        <el-form-item label="分类名称" label-width="100px" prop="input">
          <el-input placeholder="请输入内容" v-model="form.input"> </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFlag = false">取 消</el-button>
        <el-button type="primary" @click="confirmEdit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 点击删除的提示框 -->
    <el-dialog title="提示" :visible.sync="deleteFlag" width="30%">
      <span>
        <i class="el-icon-warning"></i>
        此操作将删除该分类, 是否继续?</span
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="noDelete">取 消</el-button>
        <el-button type="primary" @click="deleteConfirm">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加商品的弹出框 -->
    <el-dialog title="添加分类" :visible.sync="addFlag">
      <el-form :model="addForm" :rules="rules">
        <el-form-item label="分类名称" prop="cat_name" label-width="100px">
          <el-input placeholder="请输入内容" v-model="addForm.cat_name">
          </el-input>
        </el-form-item>
        <el-form-item label="父级分类" label-width="100px">
          <el-cascader
            :options="options"
            :props="{ checkStrictly: true }"
            @change="handleChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addFlag = false">取 消</el-button>
        <el-button type="primary" @click="addFn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PageView from "@/components/PageView.vue";
import $ from "@/api/goods/goods";
export default {
  name: "TableView",
  data() {
    return {
      addFlag: false,
      editFlag: false,
      deleteFlag: false,
      pagenum: 1,
      pagesize: 10,
      addForm: {
        cat_name: "",
        f_cat_name: "",
        pid: "",
      },
      form: {
        input: "",
        id: "",
      },
      rules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
        input: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
      },
      tableData: [],
      options: [],
      total: 0,
      type: 3,
    };
  },
  created() {
    this.get_data(this.type, this.pagenum, this.pagesize);
  },
  mounted() {
    this.$bus.$on("changeNum", (val) => {
      this.pagenum = val;
      this.get_data(this.type, this.pagenum, this.pagesize);
    });
    this.$bus.$on("changeSize", (val) => {
      this.pagesize = val;
      this.get_data(this.type, this.pagenum, this.pagesize);
    });
  },
  methods: {
    //添加商品
    async addToast() {
      this.addFlag = true;
      let res = await $.getCateApi(2);
      let arr = [];
      res.forEach((item) => {
        let obj = {};
        obj["value"] = item.cat_id;
        obj["label"] = item.cat_name;
        obj["children"] = [];
        if (item.children) {
          item.children.forEach((cItem) => {
            let cobj = {};
            cobj["value"] = cItem.cat_id;
            cobj["label"] = cItem.cat_name;
            obj["children"].push(cobj);
          });
        }
        arr.push(obj);
      });
      this.options = arr;
    },
    async addFn() {
      let cat_level = this.addForm.f_cat_name.length - 1;
      let cat_name = this.addForm.cat_name;
      let cat_pid = this.addForm.f_cat_name[cat_level];
      await $.addCateApi(cat_level, cat_name, cat_pid);
      this.$message("添加分类成功");
      this.addFlag = false;
      this.addForm.f_cat_name = "";
      this.addForm.cat_name = "";
      this.get_data(this.type, this.pagenum, this.pagesize);
    },
    handleChange(value) {
      this.addForm.f_cat_name = value;
    },
    //删除
    deleteTip(id) {
      this.deleteFlag = true;
      this.form.id = id;
    },
    noDelete() {
      this.$message("取消删除");
    },
    async deleteConfirm() {
      await $.deleteCateApi(this.form.id);
      this.deleteFlag = false;
      this.$message("删除成功");
      this.form.id = "";
      this.get_data(this.type, this.pagenum, this.pagesize);
    },
    //编辑
    edit(id, name) {
      this.editFlag = true;
      (this.form.id = id), (this.form.input = name);
    },
    async confirmEdit() {
      await $.editCateApi(this.form.id, this.form.input);
      this.editFlag = false;
      this.form.id = "";
      this.form.input = "";
      this.get_data(this.type, this.pagenum, this.pagesize);
    },
    async get_data(type, num, size) {
      let res = await $.getCateApi(type, num, size);
      this.tableData = res.result;
      this.total = res.total;
    },
  },
  components: { PageView },
};
</script>

<style lang="scss" scoped>
.box-card {
  margin-top: 10px;
}
.addGoods {
  margin-bottom: 10px;
}
.el-icon-success {
  color: rgb(47, 255, 50);
  background: #fff;
  border-radius: 50%;
}
.el-icon-warning {
  color: orange;
  background: #fff;
  border-radius: 50%;
  margin-right: 4px;
}
</style>